package com.xydtech.ecard.ecard.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.ecard.domain.EcardApiExperienceUser;
import com.xydtech.ecard.ecard.mapper.EcardApiExperienceUserMapper;
import com.xydtech.ecard.ecard.service.IEcardApiExperienceUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电子名片用户申请体验Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-10-23 上午8:54
 */
@Service
public class EcardApiExperienceUserServiceImpl implements IEcardApiExperienceUserService {

    @Autowired
    private EcardApiExperienceUserMapper ecardExperienceUserMapper;

    /**
     * 保存用户申请体验数据
     * @param ecardExperienceUser
     * @return
     */
    @Override
    public int addExperienceUserInfo(EcardApiExperienceUser ecardExperienceUser, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardExperienceUser.getExpPhone())) {
                ecardExperienceUser.setExpPhone(AESEncryptionUtils.encrypt(ecardExperienceUser.getExpPhone(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardExperienceUserMapper.addExperienceUserInfo(ecardExperienceUser);
    }

    /**
     * 查询电子名片申请体验用户列表
     * @param ecardExperienceUser
     * @return
     */
    @Override
    public List<EcardApiExperienceUser> getExperienceUserList(EcardApiExperienceUser ecardExperienceUser, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardExperienceUser.getExpPhone())) {
                ecardExperienceUser.setExpPhone(AESEncryptionUtils.encrypt(ecardExperienceUser.getExpPhone(), salt));
            }
            List<EcardApiExperienceUser> experienceUserList = ecardExperienceUserMapper.getExperienceUserList(ecardExperienceUser);
            if (experienceUserList.size() > 0) {
                for (EcardApiExperienceUser experienceUser : experienceUserList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(experienceUser.getExpPhone())) {
                        experienceUser.setExpPhone(AESEncryptionUtils.decrypt(experienceUser.getExpPhone(), salt));
                    }
                }
            }
            return experienceUserList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询用户申请体验数据详情
     * @param id
     * @return
     */
    @Override
    public EcardApiExperienceUser getExperienceUserInfo(Long id, String salt) {
        EcardApiExperienceUser experienceUserInfo = ecardExperienceUserMapper.getExperienceUserInfo(id);
        if (experienceUserInfo != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(experienceUserInfo.getExpPhone())) {
                    experienceUserInfo.setExpPhone(AESEncryptionUtils.decrypt(experienceUserInfo.getExpPhone(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return experienceUserInfo;
    }

    /**
     * 更新用户申请体验数据
     * @param ecardExperienceUser
     * @return
     */
    @Override
    public int updateEcardExperienceUser(EcardApiExperienceUser ecardExperienceUser, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardExperienceUser.getExpPhone())) {
                ecardExperienceUser.setExpPhone(AESEncryptionUtils.encrypt(ecardExperienceUser.getExpPhone(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //更新申请体验数据状态
        return ecardExperienceUserMapper.updateEcardExperienceUser(ecardExperienceUser);
    }

    /**
     * 根据openid和手机号查询用户申请体验数据
     * @param openId
     * @param expPhone
     * @return
     */
    @Override
    public List<EcardApiExperienceUser> verifyExperienceUser(String openId, String expPhone, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(expPhone)) {
                expPhone = (AESEncryptionUtils.encrypt(expPhone, salt));
            }
            List<EcardApiExperienceUser> experienceUserList = ecardExperienceUserMapper.verifyExperienceUser(openId, expPhone);
            if (experienceUserList.size() > 0) {
                for (EcardApiExperienceUser experienceUser : experienceUserList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(experienceUser.getExpPhone())) {
                        experienceUser.setExpPhone(AESEncryptionUtils.decrypt(experienceUser.getExpPhone(), salt));
                    }
                }
            }
            return experienceUserList;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
